﻿using System;
using System.Web.UI;
using System.Drawing;
using DAL.POCO;
using DAL.Repositories;

namespace GameAdmin.Pages
{
    public partial class level : System.Web.UI.Page
    {
        protected void Page_Init(object sender, EventArgs e)
        {
            btnSave.Click += new EventHandler(btnSave_Click);
            btnSaveAndDesign.Click += new EventHandler(btnSaveAndDesign_Click);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (RouteData.Values["id"] != null) ShowOriginalValues();
            }
        }

        void btnSaveAndDesign_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    int id = SaveToDb();
                    RedirectToMapDesign(id);
                }
                catch (Exception ex)
                {
                    lblMessage.Visible = true;
                    lblMessage.Text = ex.Message;
                }
            }
        }

        void btnSave_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    SaveToDb();
                    Response.Redirect("~/levels", true);
                }
                catch (Exception ex)
                {
                    lblMessage.Visible = true;
                    lblMessage.Text = ex.Message;
                }
            }
        }

        private int SaveToDb()
        {
            Round r = new Round();
            r.id = Int32.Parse(txtId.Text);
            r.playerX = Int32.Parse(txtPlayerX.Text);
            r.playerY = Int32.Parse(txtPlayerY.Text);
            r.width = Int32.Parse(txtWidth.Text);
            r.height = Int32.Parse(txtHeight.Text);
            r.time = Int32.Parse(txtTime.Text);

            Validate(r); // throws exception if something's wrong, handled in click handlers and displayed to the client

            if (txtId.ReadOnly)
            {
                RoundRepository.Update(r);
            }
            else
            {
                RoundRepository.Insert(r);
            }

            return r.id;
        }

        private void Validate(Round r)
        {
            if (r.width > 35 || r.height > 35) throw new ArgumentOutOfRangeException("Musíte zadat rozměry max. 35x35!");
            if (r.playerX <= 0 || r.playerX >= r.width) throw new ArgumentOutOfRangeException("Výchozí souřadnice X může být jen v rozmezí 1 - " + (r.width - 2).ToString());
            if (r.playerY <= 0 || r.playerY >= r.height) throw new ArgumentOutOfRangeException("Výchozí souřadnice Y může býy jen v rozmezí 1 - " + (r.height - 2).ToString());
        }

        private void RedirectToMapDesign(int id)
        {
            Response.Redirect("~/mapdesigner/" + id.ToString(), true);
        }

        private void ShowOriginalValues()
        {
            int id = Int32.Parse((string)RouteData.Values["id"]);

            Round r = RoundRepository.GetRound(id);

            txtId.Text = r.id.ToString();
            txtPlayerX.Text = r.playerX.ToString();
            txtPlayerY.Text = r.playerY.ToString();
            txtWidth.Text = r.width.ToString();
            txtHeight.Text = r.height.ToString();
            txtTime.Text = r.time.ToString();

            txtId.ReadOnly = true;
            txtId.BackColor = Color.Silver;
        }
    }
}